#include "yuechuan.h"
#include <regex>

void test()
{
    std::string pattern("([[:alpha:]])+\\.(cpp|cc)");
    std::cmatch match;
    try{
        std::regex r(pattern, std::regex::icase);
        std::regex_search("hello.cpp123", match, r);
        if(!match.empty())
        {
           std::cout<<match.str(0)<<std::endl;
           std::cout<<match.str(1)<<std::endl;
           std::cout<<match.str(2)<<std::endl;

        }
    }catch(std::regex_error &e)
    {
        std::cout<<"not found"<<std::endl;
    }
    

}

int main(int argc, char **argv)
{
  
    try
    {
        std::string pattern("[^c]ie");
        pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
        std::regex r(pattern, std::regex::icase);
        std::smatch result;
        std::string str("12312312312312312312friend123131231231231thrief3123123123123ecie");
        // if(std::regex_search(str, result, r))
        // {// only find the first one
        // YUECHUAN_LOG_INFO(YUECHUAN_LOG_ROOT()) << result.str();
        // }else{
        //     std::cout<<"not found "<<std::endl;
        // }
        std::sregex_iterator it(str.begin(),str.end(),r);
        std::sregex_iterator end_it;
        for(;it!= end_it; ++it)
        {
            int pos = it->prefix().length();
            pos = pos>40? pos-40:0;
            std::cout<<it->prefix().str().substr(pos) <<"\n\t\t>>>"<<it->str()<<" <<< \n" 
            <<it->prefix().str().substr(0,40)<<std::endl;
            std::cout<<(*it).length()<<std::endl;
            std::cout<<(*it).position(0)<<std::endl;
        }
        
    }
    catch (std::regex_error &e)
    {
        std::cout<<e.what()<< " -- " << e.code() << std::endl;
    }
test();
    return 0;
}
